#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef WIN32
#include "windows.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "AWLocalViewport.h"

extern int SCREEN_DX,SCREEN_DY;


AW_LocalViewport::AW_LocalViewport(int x,int y,int dx,int dy)
{
	m_viewport[0]=x;
	m_viewport[1]=y;
	m_viewport[2]=dx;
	m_viewport[3]=dy;
} /* AW_LocalViewport::AW_LocalViewport */ 


AW_LocalViewport::~AW_LocalViewport()
{
} /* AW_LocalViewport::~AW_LocalViewport */ 


int AW_LocalViewport::get_x()
{
	return m_viewport[0];
} /* AW_LocalViewport::get_x */ 


int AW_LocalViewport::get_y()
{
	return m_viewport[1];
} /* AW_LocalViewport::get_y */ 


int AW_LocalViewport::get_dx()
{
	return m_viewport[2];
} /* AW_LocalViewport::get_dx */ 


int AW_LocalViewport::get_dy()
{
	return m_viewport[3];
} /* AW_LocalViewport::get_dy */ 


void AW_LocalViewport::draw(void)
{
	GLint vp[4];

	glGetIntegerv(GL_VIEWPORT,vp);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	{
		float tmpls[4]={0.0F,0.0F,0.0F,1.0F};
		float tmpld[4]={0.2F,0.2F,0.2F,1.0F};
		float tmpla[4]={0.2F,0.2F,0.2F,1.0F};
		float lightpos[4]={0,0,0,1.0F};

		glEnable(GL_LIGHT7);
		glLightfv(GL_LIGHT7,GL_AMBIENT,tmpla);
		glLightfv(GL_LIGHT7,GL_DIFFUSE,tmpld);
		glLightfv(GL_LIGHT7,GL_SPECULAR,tmpls);
		lightpos[0]=0;
		lightpos[1]=0;
		lightpos[2]=-10;
		glLightfv(GL_LIGHT7,GL_POSITION,lightpos);
	}

	glViewport(m_viewport[0],m_viewport[1],m_viewport[2],m_viewport[3]);

	glEnable(GL_SCISSOR_TEST);
	glScissor(m_viewport[0],m_viewport[1],m_viewport[2],m_viewport[3]);

	draw_internal();

	glDisable(GL_LIGHT7);

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glDisable(GL_SCISSOR_TEST);
	glScissor(vp[0],vp[1],vp[2],vp[3]);
	glViewport(vp[0],vp[1],vp[2],vp[3]);
} /* AW_LocalViewport::draw */  


void AW_LocalViewport::draw_internal(void)
{

} /* AW_LocalViewport::draw_internal */  




bool AW_LocalViewport::MouseClick(int x,int y)
{
	GLint vp[4]={0,0,SCREEN_DX,SCREEN_DY};
	GLuint buffer[1024];
	unsigned int hits;
	float fAspect;

	glGetIntegerv(GL_VIEWPORT,vp);

	glSelectBuffer(1024,buffer); 

	glRenderMode(GL_SELECT);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	glViewport(m_viewport[0],m_viewport[1],m_viewport[2],m_viewport[3]);

	gluPickMatrix(x,SCREEN_DY-y,1,1,m_viewport);
	if (SCREEN_DY!=0) fAspect = (float)SCREEN_DX/SCREEN_DY;
				 else fAspect = 1.0f;	

	draw_internal();

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glViewport(vp[0],vp[1],vp[2],vp[3]);

	hits = glRenderMode(GL_RENDER);
	if (hits>0) {
		unsigned int i;
		GLuint names, *ptr, minZ,*ptrNames=0, numberOfNames=0;

		ptr = (GLuint *) buffer;
		minZ = 0xffffffff;
		for (i = 0; i < hits; i++) {	
			names = *ptr;
			ptr++;
			if (*ptr < minZ) {
				numberOfNames = names;
				minZ = *ptr;
				ptrNames = ptr+2;
			} /* if */ 

			ptr += names+2;
		} /* for */ 

		ptr = ptrNames;

		if (numberOfNames>0) return MouseClick_internal(*ptr);
	} /* if */ 

	return true;
} /* AW_LocalViewport::MouseClick */ 


bool AW_LocalViewport::MouseClick_internal(int)
{
	return false;
} /* AW_LocalViewport::MouseClick_internal */ 


